<template>
	<view class="files">
		<view class="fileinfo">
			<view class="submeida">
				<view class="medialine">
					<view class="mediaarea">
						<view class="file" @click="btnclick('file')"></view>
					</view>
					<view class="filetext">{{$t('baseCommon.buttons.selectFile')}}</view>
				</view>
				<view class="medialine">
					<view class="mediaarea">
						<view class="photo" @click="btnclick('camera')"></view>
					</view>
					<view class="phototext">{{$t('baseCommon.buttons.photosVideos')}}</view>
				</view>
				<view class="medialine">
					<view class="mediaarea">
						<view class="audio" @click="btnclick('audio')"></view>
					</view>
					<view class="audiotext" >{{$t('baseCommon.buttons.recording')}}</view>
				</view>
			</view>
			
			<view class="divide"></view>
			
			<view>
				<file-list :files="files" :hasdeleted="hasdeleted" emitkey="file_defect_del" @modifyName="fileModifyName"></file-list>
			</view>
		</view>
	</view>
</template>

<script>
	import FileList from "@/components/business/attach-file/file-list-offline.vue"
	import * as sqliteUtil from '@/utils/sqlite.js'
	
	export default {
		props: {
			files: {
				type: Array,
				default() {
					return []
				}
			},
			hasdeleted: {
				type: Boolean,
				default: false
			}
		},
		components: {
			FileList
		},
		data() {
			return {
				
			}
		},
		methods: {
			btnclick(type) {
				const _this = this
				if(type == "file"){
					uni.chooseImage({
					    sizeType: ['original'],
					    sourceType: ['album'],
					    success: function (res) {
							_this.uploadImages(res.tempFilePaths)
					    }
					});
				}else if(type == "camera"){
					// #ifdef APP-PLUS
					const camera = plus.camera.getCamera();
					// #endif
					uni.showActionSheet({
					    itemList: [_this.$t('baseCommon.buttons.photo'), _this.$t('baseCommon.buttons.video')],
					    success: function (res) {
							if(res.tapIndex === 0){ //拍照
								var resolution = camera.supportedImageResolutions[0];
								var format = camera.supportedImageFormats[0];
								camera.captureImage(function(path){
									_this.uploadFile(path)
								},function(error) {
									console.log("Capture image failed: " + error.message);
								},{resolution:resolution, format:format});
							}else{ //录像
								var resolution = camera.supportedVideoResolutions[0];
								var format = camera.supportedVideoFormats[0];
								camera.startVideoCapture(function(path){
									_this.uploadFile(path)
								},function(error) {
									console.log("Capture video failed: " + error.message);
								},{resolution:resolution, format:format});
							}
					    },
					    fail: function (res) {
					        console.log(res.errMsg);
					    }
					});
				}else if(type == "audio"){
					uni.navigateTo({
						url: "/pages/base/voice-offline?emitkey=file_defect_add"
					})
				}
			},
			async uploadImages(files) {
				let addFiles = []
				for(let file of files){
					let ext = file.substring(file.lastIndexOf(".")+1)
					let fileName = new Date().getTime()
					let addFile = {
						name: fileName,
						filePath: '',
						ext: ext,
						uploadState: '0'
					}
					
					uni.saveFile({
						tempFilePath: file,
						success: function (res) {
							addFile.filePath = res.savedFilePath
							sqliteUtil.insertMCpisFileAttachment(addFile).then(result=>{
								addFile.id = result
							})
							addFiles.push(addFile)
						}
					});
				}
				setTimeout(()=>{
					uni.$emit("images_defect_add", addFiles)
				}, 500)
			},
			async uploadFile(item) {
				let ext = item.split('.')[1]
				let fileName = new Date().getTime()
				let addFile = {
					name: fileName,
					filePath: '',
					ext: ext,
					uploadState: '0'
				}
				
				uni.saveFile({
					tempFilePath: item,
					success: function (res) {
						addFile.filePath = res.savedFilePath
						sqliteUtil.insertMCpisFileAttachment(addFile).then(result=>{
							addFile.id = result
						})
					}
				});
				setTimeout(()=>{
					uni.$emit("file_defect_add", addFile)
				}, 500)
			},
			fileModifyName(data) {
				this.$emit("modifyName", data)
			}
		}
	}
</script>

<style>
	.files {
		/* padding: 16rpx 0; */
	}
	
	.fileinfo {
		background-color: #FFFFFF;
		padding: 0 10px;
	}
	
	.submeida{
		width: 100%;
		display: flex;
		padding: 30px 0;
	}
	
	.medialine{
		flex: 1;
		text-align: center;
	}
	
	.mediaarea{
		width: 60px;
		height: 60px;
		border-radius: 50%;
		background-color: #EFEFEF;
		margin: 0 auto;
		padding: 10px;
	}
	
	.file{
		width: 40px;
		height: 40px;
		background: url(../../../../../static/images/icon/folder.png) no-repeat center center;
		background-size: 100% 100%;
	}
	
	.filetext{
		font-size: 14px;
		color: #333333;
		padding-top: 8px;
	}
	
	.photo{
		width: 40px;
		height: 40px;
		background:url(../../../../../static/images/icon/camera.png);
		background-size: 100% 100%;
	}
	
	.phototext{
		font-size: 14px;
		color: #333333;
		padding-top: 8px;
	}
	
	.audio{
		width: 40px;
		height: 40px;
		background:url(../../../../../static/images/icon/microphone.png);
		background-size: 100% 100%;
	}
	
	.audiotext{
		font-size: 14px;
		color: #333333;
		padding-top: 8px;
	}
	
	.divide{
		border-top: solid 1px #EFEFEF;
	}
</style>
